/****************************
 * @author Laureta Xhaferaj
 * @email laureta@first.al
 * @date 
 * 
 * additional scripts
 */
jQuery(document).ready(function() {

    var vendi = window.location.pathname;
    vendi = vendi.split("/");
    vendi = vendi[vendi.length - 1];
    if (vendi === '')
    {
        $("#tab_home").addClass("active");
    }
    else if (vendi === 'vehicle_maintenance')
    {
        $("#tab_cars").addClass("active");
    }
    $('#dataTable').DataTable({
    });
    jQuery('.slider').each(function() {
        jQuery(this).find('.slider-div').slider({
            animate: true,
            range: "min",
            value: jQuery('.fuel').val(),
            min: 0,
            max: 10,
            step: 0.1,
            slide: function(event, ui) {

                $(this).parent().find('.fuel').val(ui.value);
            }
        });
    });
    $(function() {
        $(document).tooltip();
    });
    $('.delete').click(function(ev) {

        var href = $(this).attr('href');
        if (!$('#dataConfirmModal').length) {
            $('body').append('#dataConfirmModal');
        }
        $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
        $('#dataConfirmOK').attr('href', href);
        $('#dataConfirmModal').modal({show: true});
        return false;
    });
    $('.pay').click(function(ev) {

        var href = $(this).attr('href');
        if (!$('#dataConfirmModal').length) {
            $('body').append('#dataConfirmModal');
        }
        $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
        $('#dataConfirmOK').attr('href', href);
        $('#dataConfirmModal').modal({show: true});
        return false;
    });
    $(document).on('change', '#repair_field', function() {
        value = $("#repair_field").val();
        if (value === 15) {
            $("#other_repair").removeAttr('readonly');
        } else if (value !== 15) {
            $("#other_repair").attr('readonly', true);
        }
    });
    $('#print').prepend('<a class="btn btn-default btn-circle" href="#print">\n\
    <i class="glyphicon glyphicon-print"></i></a>');
    $('#print').click(function() {
        window.print();
        return false;
    });
    $('#res-btn').on('click', function() {
        var val1 = $('input[name$="nrdays"]').val();
        var val2 = $('input[name$="rate"]').val();
        var result = val1 * val2;
        $('input[name="result"]').val(result);
    });
    $("#print-test").on("click", function() {
        var divContents = $("#divContainer").html();
        var printWindow = window.open('', '', 'height=700,width=1000');
        printWindow.document.write('<html><head><title>DIV Contents</title>');
        printWindow.document.write('</head><body>');
        printWindow.document.write(divContents);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.print();
    });
    
    $("#ex_customers").on('click', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: '../../existing_customers',
            success: function(data) {
                $('#div-1').hide();
                str = [];
                str.push('<br/><table id="dataTable" style="width:100%" class="table table-bordered table-condensed table-hover table-striped"><thead style="width:100%">\n\
                        <tr>\n\
                          <th>Emri</th>\n\
                          <th>Mbiemri</th>\n\
                          <th>Pasaporta</th>\n\
                          <th>Adresa</th>\n\
                          <th>Qyteti</th>\n\
                          <th>Shteti</th>\n\
                          <th>Email</th>\n\
                          <th>Phone</th>\n\
                          <th>Workphone</th>\n\
                          <th>CompanyName</th>\n\
                          <th>DOB</th>\n\
                          <th>License Category</th>\n\
                          <th>Expire</th>\n\
                          <th>Lic Nr</th>\n\
                        </tr>\n\
                     </thead>\n\
                     <tbody style="width:100%">');
                $.each(data, function(i, item) {
                    if(item.companyname == null){companyname = ''}else{companyname = item.companyname}
                    if(item.drfirstname == null){drfirstname = ''}else{drfirstname = item.drfirstname}
                    if(item.drlastname == null){drlastname = ''}else{drlastname = item.drlastname}
                    if(item.drpasportid == null){drpasportid = ''}else{drpasportid = item.drpasportid}
                    if(item.draddress == null){draddress = ''}else{draddress = item.draddress}
                    if(item.drcity == null){drcity = ''}else{drcity = item.drcity}
                    if(item.drcountry == null){drcountry = ''}else{drcountry = item.drcountry}
                    if(item.DREMAIL == null){DREMAIL = ''}else{DREMAIL = item.DREMAIL}
                    if(item.drhphone == null){drhphone = ''}else{drhphone = item.drhphone}
                    if(item.workphone == null){workphone = ''}else{workphone = item.workphone}
                    if(item.drbdate == null){drbdate = ''}else{drbdate = item.drbdate}
                    if(item.drliccat == null){drliccat = ''}else{drliccat = item.drliccat}
                    if(item.drlexpdate == null){drlexpdate = ''}else{drlexpdate = item.drlexpdate}
                    if(item.drlicnr == null){drlicnr = ''}else{drlicnr = item.drlicnr}
                    str.push('<tr class="customer-tab" ><td class="drfirstname">' +
                        drfirstname + '</td>\n\
                    <td>' + drlastname + '</td>\n\
                    <td>' + drpasportid + '</td>\n\
                    <td>' + draddress + '</td>\n\
                    <td>' + drcity + '</td>\n\
                    <td>' + drcountry + '</td>\n\
                    <td>' + DREMAIL + '</td>\n\
                    <td>' + drhphone + '</td>\n\
                    <td>' + workphone + '</td>\n\
                    <td>' + companyname + '</td>\n\
                    <td>' + drbdate + '</td>\n\
                    <td>' + drliccat + '</td>\n\
                    <td>' + drlexpdate + '</td>\n\
                    <td>' + drlicnr + '</td>\n\
                    </tr>');
                });
                str.push('</tbody></table>');
                document.getElementById("div-customer").innerHTML += str.join("");
                $('#dataTable').DataTable({
                    "bPaginate": true,
                    "bSort": true,
                    "searching": true

                });
                var nrdays = $('input[name="nrdays"]').val();
                var result = $('input[name="result"]').val();
                var rate = $('input[name="rate"]').val();
                var vehicle = $('input[name="vehicle"]').val();
                var rentalId = $('input[name="rentalId"]').val();
                $('.customer-tab').on('click', function() {
                    var str = new Array();
                    var t = $(this);
                    str.push(t.find('td:not(:empty):first').html());
                    str.push(t.find('td:nth-child(2)').html());
                    str.push(t.find('td:nth-child(3)').html());
                    str.push(t.find('td:nth-child(4)').html());
                    str.push(t.find('td:nth-child(5)').html());
                    str.push(t.find('td:nth-child(6)').html());
                    str.push(t.find('td:nth-child(7)').html());
                    str.push(t.find('td:nth-child(8)').html());
                    str.push(t.find('td:nth-child(9)').html());
                    str.push(t.find('td:nth-child(10)').html());
                    str.push(t.find('td:nth-child(11)').html());
                    str.push(t.find('td:nth-child(12)').html());
                    str.push(nrdays); //12
                    str.push(result); //13
                    str.push(rate); //14
                    str.push(vehicle); //15
                    str.push(t.find('td:nth-child(13)').html()); //16
                    str.push(t.find('td:nth-child(14)').html()); //17
                    str.push(rentalId); //18

                    var data = JSON.stringify(str);
                    window.location.href = '../../customer_rental/' + data;
                });
            }
        });
    });
        
        $("#customers_rental").on('click', function(e) {

            e.preventDefault();
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '../../existing_customers',
                success: function(data) {
                    $('#div-1').hide();
                    str = [];
                    str.push('<br/><table id="dataTable" style="width:100%" class="table table-bordered table-condensed table-hover table-striped"><thead  style="width:100%">\n\
                        <tr>\n\
                          <th>Emri</th>\n\
                          <th>Mbiemri</th>\n\
                          <th>Pasaporta</th>\n\
                          <th>Adresa</th>\n\
                          <th>Qyteti</th>\n\
                          <th>Shteti</th>\n\
                          <th>Email</th>\n\
                          <th>Phone</th>\n\
                          <th>Workphone</th>\n\
                          <th>CompanyName</th>\n\
                          <th>DOB</th>\n\
                          <th>License Category</th>\n\
                          <th>Expire</th>\n\
                          <th>Lic Nr</th>\n\
                        </tr>\n\
                     </thead>\n\
                     <tbody  style="width:100%">');
                    $.each(data, function(i, item) {
                        if(item.companyname == null){companyname = ''}else{companyname = item.companyname}
                        if(item.drfirstname == null){drfirstname = ''}else{drfirstname = item.drfirstname}
                        if(item.drlastname == null){drlastname = ''}else{drlastname = item.drlastname}
                        if(item.drpasportid == null){drpasportid = ''}else{drpasportid = item.drpasportid}
                        if(item.draddress == null){draddress = ''}else{draddress = item.draddress}
                        if(item.drcity == null){drcity = ''}else{drcity = item.drcity}
                        if(item.drcountry == null){drcountry = ''}else{drcountry = item.drcountry}
                        if(item.DREMAIL == null){DREMAIL = ''}else{DREMAIL = item.DREMAIL}
                        if(item.drhphone == null){drhphone = ''}else{drhphone = item.drhphone}
                        if(item.workphone == null){workphone = ''}else{workphone = item.workphone}
                        if(item.drbdate == null){drbdate = ''}else{drbdate = item.drbdate}
                        if(item.drliccat == null){drliccat = ''}else{drliccat = item.drliccat}
                        if(item.drlexpdate == null){drlexpdate = ''}else{drlexpdate = item.drlexpdate}
                        if(item.drlicnr == null){drlicnr = ''}else{drlicnr = item.drlicnr}
                        str.push('<tr class="customer-tab" ><td class="drfirstname">' +
                                drfirstname + '</td>\n\
                    <td>' + drlastname + '</td>\n\
                    <td>' + drpasportid + '</td>\n\
                    <td>' + draddress + '</td>\n\
                    <td>' + drcity + '</td>\n\
                    <td>' + drcountry + '</td>\n\
                    <td>' + DREMAIL + '</td>\n\
                    <td>' + drhphone + '</td>\n\
                    <td>' + workphone + '</td>\n\
                    <td>' + companyname + '</td>\n\
                    <td>' + drbdate + '</td>\n\
                    <td>' + drliccat + '</td>\n\
                    <td>' + drlexpdate + '</td>\n\
                    <td>' + drlicnr + '</td>\n\
                    </tr>');
                    });
                    str.push('</tbody></table>');
                    document.getElementById("div-customer").innerHTML += str.join("");
                    $('#dataTable').DataTable({
                        "bPaginate": true,
                        "bSort": true,
                        "searching": true

                    });
                    var nrdays = $('input[name="nrdays"]').val();
                    var result = $('input[name="result"]').val();
                    var rate = $('input[name="rate"]').val();
                    var vehicle = $('input[name="vehicle"]').val();
                    var startdate = $('input[name="startdate"]').val();
                    var enddate = $('input[name="enddate"]').val();
                    
                    $('.customer-tab').on('click', function() {
                        var str = new Array();
                        var t = $(this);
                        str.push(t.find('td:not(:empty):first').html());
                        str.push(t.find('td:nth-child(2)').html());
                        str.push(t.find('td:nth-child(3)').html());
                        str.push(t.find('td:nth-child(4)').html());
                        str.push(t.find('td:nth-child(5)').html());
                        str.push(t.find('td:nth-child(6)').html());
                        str.push(t.find('td:nth-child(7)').html());
                        str.push(t.find('td:nth-child(8)').html());
                        str.push(t.find('td:nth-child(9)').html());
                        str.push(t.find('td:nth-child(10)').html());
                        str.push(t.find('td:nth-child(11)').html());
                        str.push(t.find('td:nth-child(12)').html());
                        str.push(nrdays); //12
                        str.push(result); //13
                        str.push(rate); //14
                        str.push(vehicle); //15
                        str.push(t.find('td:nth-child(13)').html()); //16
                        str.push(t.find('td:nth-child(14)').html()); //17
                        str.push(startdate); //18
                        str.push(enddate); //19

                        var data = JSON.stringify(str);
                        window.location.href = '../../customer_new_rental/' + data;
                    });
                }
            });
        });
    });
